#!/usr/bin/env bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific

# description: ambari-agent daemon
# processname: ambari-agent

# /etc/init.d/ambari-agent

VERSION="${ambariVersion}"
HASH="${buildNumber}"

case "$1" in
  --version)
        echo -e $VERSION
        exit 0
        ;;
  --hash)
        echo -e $HASH
        exit 0
        ;;
esac

export PATH=/usr/lib/ambari-server/*:$PATH
export AMBARI_CONF_DIR=/etc/ambari-server/conf:$PATH

# Because Ambari rpm unpacks modules here on all systems
export PYTHONPATH=/usr/lib/python2.6/site-packages:$PYTHONPATH

AMBARI_AGENT=ambari-agent
PIDFILE=/var/run/ambari-agent/$AMBARI_AGENT.pid
OUTFILE=/var/log/ambari-agent/ambari-agent.out
LOGFILE=/var/log/ambari-agent/ambari-agent.log
AGENT_SCRIPT=/usr/lib/python2.6/site-packages/ambari_agent/main.py
AMBARI_AGENT_PY_SCRIPT=/usr/lib/python2.6/site-packages/ambari_agent/AmbariAgent.py
OK=1
NOTOK=0


if [ -a /usr/bin/python2.7 ] && [ -z "$PYTHON" ]; then
  PYTHON=/usr/bin/python2.7
fi

if [ -a /usr/bin/python2.6 ] && [ -z "$PYTHON" ]; then
  PYTHON=/usr/bin/python2.6
fi

if [ "x$PYTHON" == "x" ]; then
  PYTHON=/usr/bin/python
fi

export PYTHON=$PYTHON

# Trying to read the passphrase from an environment
if [ ! -z $AMBARI_PASSPHRASE ]; then
  RESOLVED_AMBARI_PASSPHRASE=$AMBARI_PASSPHRASE
fi

# Reading the environment file
if [ -a /var/lib/ambari-agent/ambari-env.sh ]; then
  . /var/lib/ambari-agent/ambari-env.sh
fi

if [ ! -z $AMBARI_LOG_DIR ]; then
  LOGFILE=$AMBARI_LOG_DIR/ambari-agent.log
fi

if [ -z $RESOLVED_AMBARI_PASSPHRASE ] &&  [ ! -z $AMBARI_PASSPHRASE ]; then
  RESOLVED_AMBARI_PASSPHRASE=$AMBARI_PASSPHRASE
  # If the passphrase is not defined yet, use the value from the env file
elif [ -z $RESOLVED_AMBARI_PASSPHRASE ]; then
  # Passphrase is not defined anywhere, set the default value
  RESOLVED_AMBARI_PASSPHRASE="DEV"
fi

export AMBARI_PASSPHRASE=$RESOLVED_AMBARI_PASSPHRASE

#echo $AMBARI_PASSPHRASE

# check for version
check_python_version ()
{
  echo "Verifying Python version compatibility..."
  majversion=`$PYTHON -V 2>&1 | awk '{print $2}' | cut -d'.' -f1`
  minversion=`$PYTHON -V 2>&1 | awk '{print $2}' | cut -d'.' -f2`
  numversion=$(( 10 * $majversion + $minversion))
  if (( $numversion < 26 )); then
    echo "ERROR: Found Python version $majversion.$minversion. Ambari Agent requires Python version > 2.6"
    return $NOTOK
  fi
  echo "Using python " $PYTHON
  return $OK
}

retcode=0

if [ "$(id -u)" != "0" ]; then
 echo "You can't perform this operation as non-root user. Please, re-login as root user"
 exit 0
fi

case "$1" in
  start)
        check_python_version
        if [ "$?" -eq "$NOTOK" ]; then
          exit -1
        fi
        echo "Checking for previously running Ambari Agent..."
        if [ -f $PIDFILE ]; then
          PID=`cat $PIDFILE`
          if [ -z "`ps ax -o pid | grep $PID`" ]; then
            echo "$PIDFILE found with no process. Removing $PID..."
            rm -f $PIDFILE
          else
            tput bold
            echo "ERROR: $AMBARI_AGENT already running"
            tput sgr0
            echo "Check $PIDFILE for PID."
            exit -1
          fi
        fi
        echo "Starting ambari-agent"
        nohup $PYTHON $AMBARI_AGENT_PY_SCRIPT "$@" > $OUTFILE 2>&1 &
        sleep 2
        PID=$!
        echo "Verifying $AMBARI_AGENT process status..."
        if [ -z "`ps ax -o pid | grep $PID`" ]; then
          if [ -s $OUTFILE ]; then
            echo "ERROR: $AMBARI_AGENT start failed. For more details, see $OUTFILE:"
            echo "===================="
            tail -n 10 $OUTFILE
            echo "===================="
          else
            echo "ERROR: $AMBARI_AGENT start failed"
          fi
          echo "Agent out at: $OUTFILE"
          echo "Agent log at: $LOGFILE"
          exit -1
        fi
        tput bold
        echo "Ambari Agent successfully started"
        tput sgr0
        echo "Agent PID at: $PIDFILE"
        echo "Agent out at: $OUTFILE"
        echo "Agent log at: $LOGFILE"
        ;;
  status)
        if [ -f $PIDFILE ]; then
          PID=`cat $PIDFILE`
          echo "Found $AMBARI_AGENT PID: $PID"
          if [ -z "`ps ax -o pid | grep $PID`" ]; then
            echo "$AMBARI_AGENT not running. Stale PID File at: $PIDFILE"
            retcode=2
          else
            tput bold
            echo "$AMBARI_AGENT running."
            tput sgr0
            echo "Agent PID at: $PIDFILE"
            echo "Agent out at: $OUTFILE"
            echo "Agent log at: $LOGFILE"
          fi
        else
          tput bold
          echo "$AMBARI_AGENT currently not running"
          tput sgr0
          echo "Usage: /usr/sbin/ambari-agent {start|stop|restart|status}"
          retcode=3
        fi
        ;;
  stop)
        check_python_version
        if [ "$?" -eq "$NOTOK" ]; then
          exit -1
        fi
        if [ -f $PIDFILE ]; then
          PID=`cat $PIDFILE`
          echo "Found $AMBARI_AGENT PID: $PID"
          if [ -z "`ps ax -o pid | grep $PID`" ]; then
            tput bold
            echo "ERROR: $AMBARI_AGENT not running. Stale PID File at: $PIDFILE"
            tput sgr0
          else
            echo "Stopping $AMBARI_AGENT"
            $PYTHON $AGENT_SCRIPT stop
          fi
          echo "Removing PID file at $PIDFILE"
          rm -f $PIDFILE
          tput bold
          echo "$AMBARI_AGENT successfully stopped"
          tput sgr0
        else
          tput bold
          echo "$AMBARI_AGENT is not running. No PID found at $PIDFILE"
          tput sgr0
        fi
        ;;
  restart)
        echo -e "Restarting $AMBARI_AGENT"
        $0 stop
        $0 start "$@"
        retcode=$?
        ;;
  *)
        tput bold
        echo "Usage: /usr/sbin/ambari-agent {start|stop|restart|status}"
        tput sgr0
        retcode=1
esac

exit $retcode
